package lianbiaoti;

import jdk.nashorn.internal.ir.IfNode;

public class Test {
    static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;

    public void creatList() {
        Node node = new Node(10);
        Node node1 = new Node(11);
        Node node2 = new Node(12);
        Node node3 = new Node(13);
        Node node4 = new Node(555);
        Node node5 = new Node(14);

        node.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = null;
        head = node;


    }

    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    public void display(Node newHead) {
        Node cur = newHead;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    public void display2(Node newHead) {
        Node cur = newHead;
        while (cur != null) {
            System.out.println(cur.val);
            break;

        }
    }

    public Node addFirst(int val) {
        Node node = new Node(val);
        node.next = head;
        head = node;
        return node;
    }

    public Node addFirst2(Node node) {
        node.next = head;
        head = node;
        return node;
    }

    public Node fanZhuan() {
        if (head.next == null) {
            return head;
        }
        if (head == null) {
            return null;
        }
        Node cur = head.next;
        head.next = null;
        while (cur != null) {
            Node curnext = cur.next;

            cur.next = head;
            head = cur;
            cur = curnext;
        }
        return head;

    }

    public Node km() {
        if (head == null) {
            return null;
        }
        Node fast = head;
        Node slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }
    public int size(){
        Node cur=head;
         int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    public Node k(int k) {
        /*if (k<=0||k>size()){//k过大了
            return null;
        }*/
        if (k<=0||head==null){
            return null;
        }
        Node cur=head;
    Node fast=cur;
    Node slow=cur;
        while (k-1!=0){
            fast=fast.next;
            if (fast==null){//k过大了
                return null;
            }
            k--;
        }
    while(fast.next!=null){

        fast=fast.next;
        slow=slow.next;

    }
    return slow;

    }
    public static Node mergeTwoLists(Node list1, Node list2) {
        Node newNode=new Node(-1);
        Node tmp=newNode;
        while(list1!=null&&list2!=null){
            if (list1.val<list2.val){
                tmp.next=list1;
                list1 = list1.next;
                tmp=tmp.next;
            }else {
                tmp.next=list2;
                list2=list2.next;
                tmp=tmp.next;
            }
        }
        if (list1!=null){
            tmp.next=list1;
        }
        if (list2!=null){
            tmp.next=list2;
        }
        return newNode;
    }
    public Node partition( int x) {
        Node as=null;
        Node ae=null;
        Node bs=null;
        Node be=null;
        Node cur=head;
        while(cur!=null) {
            if (cur.val <= 10) {
                if (bs == null) {//这是第一次插入元素。0
                    bs = cur;
                    be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }

            }
            cur = cur.next;

        }
        if (bs==null){
         return as;
        }
        be.next=as;
        if (as!=null) {
            ae.next = null;
        }
        return bs;
    }
    public boolean chkPalindrome() {
        if (head==null){
            return false;
        }
        Node fast=head;
        Node slow = head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        Node cur=slow.next;
        while(cur!=null){
            Node curnext = cur.next;

            cur.next = slow;
            slow = cur;
            cur = curnext;
        }
        while (slow!=head){
            if (head.val!=slow.val){
                return false;
            }
            if (head.next==slow){
                return true;
            }
            slow=slow.next;
            head=head.next;
        }
        return true;
    }
    public Node getIntersectionNode(Node hdA, Node hdB) {
        //1.分别求两个链表的长度。
        Node cur1=hdA;
        Node cur2=hdB;
        int countA=0;
        int countB=0;
        while(hdA!=null){
            countA++;
            cur1=cur1.next;
        }
        while(hdB!=null){
            countB++;
            cur2=cur2.next;
        }
        cur1=hdA;
        cur2=hdB;
        //2.计算它的差值
        int len=countA-countB;
        if (len<0){
            cur1=hdB;
            cur2=hdA;
            len=countB-countA;
        }
        //3.让长的那个走差值步
        while(len!=0){
            cur1=cur1.next;
            len--;
        }
        //4.两个一起同时走找到交点
        while (cur1!=cur2&&cur1!=null){
            cur1=cur1.next;
            cur2=cur2.next;
        }
        if (cur1==null){
            return cur1;
        }
        return cur1;
    }
    public boolean hasCycle() {
        Node fast=head;
        Node slow=head;
        while (fast!=null&&fast.next!=null) {
            fast=fast.next.next;
            slow=slow.next;
            if (fast==slow){
                return true;
            }
        }
        return false;
    }
    public Node hasCycle2() {
        Node fast=head;
        Node slow=head;
        while (fast!=null&&fast.next!=null) {
            fast=fast.next.next;
            slow=slow.next;
            if (fast==slow){
                break;
            }
        }
        if (fast==null||fast.next==null){
         return null;
        }
       slow=head;
        while(slow!=fast){
            fast=fast.next;
            slow=slow.next;

        }
        return fast;
    }
}
